'use strict'

const { Controller } = require('egg')

class KujoMiva extends Controller {
    /**
    * @api {get} /KujoMiva 获取列表
    * @apiGroup KujoMiva
    */
    async index() {
        const { ctx } = this
        const { proid: pro_id } = ctx.header

        if (!await ctx.validate('wuyeProId', { pro_id })) return

        const result = await this.app.databaseWuye.select('advice', {
            where: { pro_id },
            orders: [['id', 'desc']]
        })

        ctx.returnBody(200, '获取反馈列表', result)
    }
    /**
     * 添加反馈
     */
    async create() {
        const { ctx, app } = this
        const { proid: pro_id } = ctx.header
        const { name, phone, opinion } = ctx.request.body
        const rule = {
            name: [
                { required: true, message: '名字不能为空' }
            ],
            phone: [
                { required: true, message: '电话不能为空' }
            ],
            opinion: [
                { required: true, message: '内容不能为空' }
            ],
        }

        if (!await ctx.validate('wuyeProId', { pro_id })) return
        if (!await ctx.validate(rule, { name, phone, opinion })) return

        const result = await app.databaseWuye.insert('advice', { pro_id, name, phone, opinion })

        ctx.returnBody(200, '反馈成功', result)
    }
    /**
     * 删除反馈
     */
    async destroy () {
        const { ctx, app } = this
        const { id } = ctx.params
        const result = await app.databaseWuye.delete('advice', { id })
        ctx.returnBody(200, '删除成功', result)
    }
}

module.exports = KujoMiva
